/*     */ package com.qmsk.business.captcha;
/*     */ 
/*     */ import java.awt.Color;
/*     */ import java.awt.Font;
/*     */ import java.awt.FontMetrics;
/*     */ import java.awt.Graphics2D;
/*     */ import java.awt.image.BufferedImage;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.util.Random;
/*     */ import javax.imageio.ImageIO;
/*     */ 
/*     */ public final class CaptchaFactory
/*     */ {
/*  21 */   private static String[] fonts = { "Jokerman", "华文彩云", 
/*  22 */     "Viner Hand ITC", "Giddyup Std", "Algerian" };
/*     */ 
/*  25 */   private static int[][] colors = { 
/*  26 */     { 247, 255, 233, 229, 244, 213, 80, 136, 29 }, 
/*  27 */     { 237, 247, 255, 212, 239, 255, 27, 95, 144 }, 
/*  28 */     { 247, 254, 236, 224, 246, 208, 181, 62 } };
/*     */ 
/*  31 */   private static int[][] heights = { 
/*  32 */     { 8, 13, 16, 19, 22, 25, 28, 32, 34, 37, 41, 43, 55, 74, 110 }, 
/*  33 */     { 6, 9, 12, 13, 16, 17, 20, 22, 23, 26, 27, 30, 39, 50, 76 }, 
/*  34 */     { 9, 13, 17, 20, 23, 26, 30, 33, 36, 39, 42, 46, 59, 78, 117 }, 
/*  35 */     { 7, 10, 14, 16, 19, 21, 24, 26, 29, 32, 34, 37, 47, 62, 92 }, 
/*  36 */     { 7, 11, 14, 16, 19, 22, 24, 27, 30, 32, 35, 38, 48, 64, 96 } };
/*     */ 
/*  41 */   private static String values = "1234567890";
/*     */ 
/*  44 */   private static Random random = new Random();
/*     */ 
/*     */   public static synchronized Captcha getCaptcha()
/*     */     throws Exception
/*     */   {
/*  50 */     return draw(Integer.valueOf(130), Integer.valueOf(50));
/*     */   }
/*     */ 
/*     */   public static synchronized Captcha getCaptcha(Integer width, Integer height) throws Exception
/*     */   {
/*  55 */     return draw(width, height);
/*     */   }
/*     */ 
/*     */   private static synchronized Captcha draw(Integer width, Integer height) throws Exception
/*     */   {
/*  60 */     if (width == null)
/*  61 */       width = Integer.valueOf(130);
/*  62 */     if (height == null) {
/*  63 */       height = Integer.valueOf(50);
/*     */     }
/*  65 */     Captcha captcha = null;
/*     */     try
/*     */     {
/*  68 */       BufferedImage image = new BufferedImage(width.intValue(), height.intValue(), 
/*  69 */         4);
/*     */ 
/*  71 */       Graphics2D graph = image.createGraphics();
/*     */ 
/*  73 */       int[] color = colors[random.nextInt(3)];
/*     */ 
/*  75 */       graph.setColor(new Color(color[0], color[1], color[2]));
/*  76 */       graph.fillRect(0, 0, width.intValue(), height.intValue());
/*     */ 
/*  78 */       graph.setColor(new Color(color[3], color[4], color[5]));
/*  79 */       graph.drawRect(0, 0, width.intValue() - 1, height.intValue() - 1);
/*     */ 
/*  81 */       graph.setColor(new Color(color[6], color[7], color[8]));
/*     */ 
/*  85 */       char[] ch = new char[4];
/*  86 */       ch[0] = values.charAt(random.nextInt(values.length()));
/*  87 */       ch[1] = values.charAt(random.nextInt(values.length()));
/*  88 */       ch[2] = values.charAt(random.nextInt(values.length()));
/*  89 */       ch[3] = values.charAt(random.nextInt(values.length()));
/*  90 */       String value = new String(ch);
/*  91 */       String paintStr = ch[0] + " " + ch[1] + " " + ch[2] + " " + ch[3];
/*     */ 
/*  95 */       int fontNum = random.nextInt(fonts.length);
/*  96 */       int[] temps = heights[fontNum];
/*     */ 
/*  98 */       int i = 0;
/*  99 */       for (; i < temps.length; ++i) {
/* 100 */         if (height.intValue() * 0.6F < temps[i])
/*     */           break;
/*     */       }
/* 103 */       --i;
/* 104 */       if (i < 0)
/* 105 */         i = 0;
/* 106 */       if (i == temps.length) {
/* 107 */         --i;
/*     */       }
/* 109 */       graph.setFont(new Font(fonts[fontNum], 1, temps[i]));
/*     */ 
/* 113 */       FontMetrics metrics = graph.getFontMetrics();
/* 114 */       int baseLine = (height.intValue() - metrics.getHeight()) / 2 + metrics
/* 115 */         .getAscent();
/* 116 */       int left = (width.intValue() - metrics.stringWidth(paintStr)) / 2;
/*     */ 
/* 120 */       graph.drawString(paintStr, left, baseLine);
/*     */ 
/* 122 */       graph.dispose();
/*     */ 
/* 124 */       ByteArrayOutputStream out = new ByteArrayOutputStream();
/*     */ 
/* 126 */       ImageIO.write(image, "png", out);
/*     */ 
/* 128 */       byte[] bytes = out.toByteArray();
/* 129 */       out.close();
/*     */ 
/* 132 */       captcha = new Captcha(value, bytes);
/*     */     } catch (Exception e) {
/* 134 */       throw new Exception(e);
/*     */     }
/* 136 */     return captcha;
/*     */   }
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\新建文件夹\WEB-INF\classes\
 * Qualified Name:     com.qmsk.business.captcha.CaptchaFactory
 * JD-Core Version:    0.5.3
 */